<?php 
	defined('C5_EXECUTE') or die(_("Access Denied."));
	$aBlocks = $controller->generateNav();
	$c = Page::getCurrentPage();
	echo("<ul class=\"nav-sitemap\">");
	
	$nh = Loader::helper('navigation');
	
	//this will create an array of parent cIDs 
	$inspectC=$c;
	$selectedPathCIDs=array( $inspectC->getCollectionID() );
	$parentCIDnotZero=true;	
	while($parentCIDnotZero){
		$cParentID=$inspectC->cParentID;
		if(!intval($cParentID)){
			$parentCIDnotZero=false;
		}else{
			$selectedPathCIDs[]=$cParentID;
			$inspectC=Page::getById($cParentID);
		}
	} 	
	
	foreach($aBlocks as $ni) {
		$_c = $ni->getCollectionObject();
		if (!$_c->getCollectionAttributeValue('exclude_nav')) {

			if (!$_c->isExternalLink()) {
				$thisLevel = $ni->getLevel();
				if ($thisLevel > $lastLevel) {
					echo("<ul>");
				} else if ($thisLevel < $lastLevel) {
					for ($j = $thisLevel; $j < $lastLevel; $j++) {
						if ($lastLevel - $j > 1) {
							echo("</li></ul>");
						} else {
							echo("</li></ul></li>");
						}
					}
				} else if ($i > 0) {
					echo("</li>");
				}
	
				$pageLink = false;
				
				if ($_c->getCollectionAttributeValue('replace_link_with_first_in_nav')) {
					$subPage = $_c->getFirstChild();
					if ($subPage instanceof Page) {
						$pageLink = $nh->getLinkToCollection($subPage);
					}
				}
				
				if (!$pageLink) {
					$pageLink = $ni->getURL();
				}
	
				if ($c->getCollectionID() == $_c->getCollectionID()) { 
					echo('<li class="nav-selected nav-path-selected"><a class="nav-selected nav-path-selected" href="' . $pageLink . '">' . $ni->getName() . '</a>');
				} elseif ( in_array($_c->getCollectionID(),$selectedPathCIDs) ) { 
					echo('<li class="nav-path-selected"><a class="nav-path-selected" href="' . $pageLink . '">' . $ni->getName() . '</a>');
				} else {
					echo('<li><a href="' . $pageLink . '">' . $ni->getName() . '</a>');
				}	
				$lastLevel = $thisLevel;
				$i++;
			}
		}
	}
	
	$thisLevel = 0;
	for ($i = $thisLevel; $i <= $lastLevel; $i++) {
		echo("</li></ul>");
	}

?>